package com.forum.web.controller.api;

import com.forum.common.annotation.Log;
import com.forum.common.core.controller.BaseController;
import com.forum.common.core.domain.AjaxResult;
import com.forum.common.core.domain.entity.SysUser;
import com.forum.common.core.domain.model.LoginUser;
import com.forum.common.core.page.TableDataInfo;
import com.forum.common.enums.BusinessType;
import com.forum.common.utils.SecurityUtils;
import com.forum.common.utils.poi.ExcelUtil;
import com.forum.system.domain.TbPost;
import com.forum.system.service.ISysUserService;
import com.forum.system.service.ITbPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 贴子Controller
 *
 * @author forum
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/api/post")
public class TbPostController extends BaseController {
    @Autowired
    private ITbPostService tbPostService;
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list/admin")
    public TableDataInfo listAdmin(TbPost tbPost) {
        startPage();
        List<TbPost> list = tbPostService.selectTbPostList(tbPost);
        if (!ObjectUtils.isEmpty(list)) {
            list.forEach(item -> {
                SysUser sysUser = sysUserService.selectUserById(item.getUserId());
                if (sysUser != null) {
                    item.setUserName(sysUser.getUserName());
                    item.setUserAvatar(sysUser.getAvatar());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 查询贴子列表
     */
//    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbPost tbPost) {
        startPage();
        tbPost.setStatus("1");
        List<TbPost> list = tbPostService.selectTbPostList(tbPost);
        if (!ObjectUtils.isEmpty(list)) {
            list.forEach(item -> {
                SysUser sysUser = sysUserService.selectUserById(item.getUserId());
                if (sysUser != null) {
                    item.setUserName(sysUser.getUserName());
                    item.setUserAvatar(sysUser.getAvatar());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出贴子列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @Log(title = "贴子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbPost tbPost) {
        List<TbPost> list = tbPostService.selectTbPostList(tbPost);
        ExcelUtil<TbPost> util = new ExcelUtil<TbPost>(TbPost.class);
        util.exportExcel(response, list, "贴子数据");
    }

    /**
     * 获取贴子详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TbPost tbPost = tbPostService.selectTbPostById(id);
        if (tbPost != null) {
            SysUser sysUser = sysUserService.selectUserById(tbPost.getUserId());
            if (sysUser != null) {
                tbPost.setUserName(sysUser.getUserName());
                tbPost.setUserAvatar(sysUser.getAvatar());
            }
        }

        return success(tbPost);
    }

    /**
     * 新增贴子
     */
    @Log(title = "贴子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPost tbPost) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbPost.setUserId(loginUser.getUser().getUserId());
        return toAjax(tbPostService.insertTbPost(tbPost));
    }

    /**
     * 修改贴子
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "贴子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPost tbPost) {
        return toAjax(tbPostService.updateTbPost(tbPost));
    }

    /**
     * 删除贴子
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "贴子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbPostService.deleteTbPostByIds(ids));
    }
}
